package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysCorp;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.dao.service.UidGenService;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.service.ISysCorpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/system/corp")
public class SycCorpController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(SycCorpController.class);
    @Autowired
    private ISysCorpService corpService;

    /**
     * 获取公司列表
     */
    @PreAuthorize("@ss.hasPermi('system:corp:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCorp corp) {
        List<SysCorp> list = corpService.selectCorpListTree(corp);
        return getDataTable(list);
    }



    /**
     * 新增公司
     */
    @PreAuthorize("@ss.hasPermi('system:corp:add')")
    @Log(title = "公司管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysCorp corp) {
        if (UserConstants.NOT_UNIQUE.equals(corpService.checkCorpNameUnique(corp.getPkCorp(), corp.getCorpName()))) {
            return AjaxResult.error("新增'" + corp.getCorpName() + "'失败，公司名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(corpService.checkCorpCodeUnique(corp.getPkCorp(), corp.getCorpCode()))) {
            return AjaxResult.error("新增'" + corp.getCorpCode() + "'失败，公司编码已存在");
        }
        return AjaxResult.success(corpService.insertCorp(corp));
    }


    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('system:corp:edit')")
    @Log(title = "公司管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysCorp corp) {
        if (UserConstants.NOT_UNIQUE.equals(corpService.checkCorpNameUnique(corp.getPkCorp(), corp.getCorpName()))) {
            return AjaxResult.error("修改'" + corp.getCorpName() + "'失败，公司名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(corpService.checkCorpCodeUnique(corp.getPkCorp(), corp.getCorpCode()))) {
            return AjaxResult.error("修改'" + corp.getCorpCode() + "'失败，公司编码已存在");
        }
        return AjaxResult.success(corpService.updateCorp(corp));
    }


    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('system:corp:remove')")
    @Log(title = "公司管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{corpIds}")
    public AjaxResult remove(@PathVariable String[] corpIds) {
        return toAjax(corpService.deleteCorpByIds(corpIds));
    }


    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:corp:edit')")
    @Log(title = "公司管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysCorp corp) {

        if(corpService.checkCorpIshaiChildren(corp.getPkCorp())){
            return AjaxResult.error("存在下级公司，禁止停用");
        }

        corp.setUpdateBy(getUsername());
        return AjaxResult.success(corpService.updateCorpStatus(corp));

    }

    /**
     * 获取公司下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysCorp corp)
    {
        List<SysCorp> list = corpService.selectCorpListTree(corp);
        return AjaxResult.success(list);
    }


}
